package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};

import com.hcyt.bridge.common.consts.Const;
import com.hcyt.bridge.common.enums.EnumCode;
import com.hcyt.bridge.common.exception.BizException;
import com.hcyt.framewrok.protocol.JsonResult;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.ArrayList;

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
@RequiredArgsConstructor
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

    @Override
    public void batchSaveOrUpdate(List<${entity}> entityList, Integer batchSize) {
        if (batchSize == null || batchSize <= 0) {
            this.baseMapper.batchInsertOrUpdate(entityList);
        } else {
            // 计算循环次数
            int times;
            times = entityList.size() / batchSize;
            if (entityList.size() % batchSize != 0){
                times ++;
            }
            // 循环分批处理数据
            for (int i = 0; i < times; i++) {
                int endIndex = (i + 1) * batchSize;
                endIndex = endIndex > entityList.size() ? entityList.size() : endIndex;
                List<${entity}> subList = entityList.subList(i * batchSize, endIndex);
                this.baseMapper.batchInsertOrUpdate(subList);
            }
        }
    }

    @Override
    public JsonResult<${entity}VO> search(Page page) {
        LambdaQueryWrapper<${entity}> queryWrapper = new LambdaQueryWrapper<>();
        // TODO search condition
        Page<${entity}> retPage = this.page(page, queryWrapper);
        List<${entity}VO> viewList = new ArrayList<>();
        retPage.getRecords().stream().forEach(entity -> {
            viewList.add(TbSysDictVO.entity2VO(entity));
        });
        return JsonResult.success()
                        .data(viewList)
                        .total(retPage.getTotal())
                        .build();
    }

    @Override
    public ${entity}VO add(${entity}VO view) {
        ${entity} entity = ${entity}VO.vo2Entity(view);
        this.save(entity);
        return ${entity}VO.entity2VO(entity);
    }

    @Override
    public void logicalDelete(Long id) {
        ${entity} entity = new ${entity}();
        entity.setId(id).setIsDelete(Const.LOGICAL_YES);
        if (!this.updateById(entity)) {
            throw new BizException(EnumCode.Err.ERR_5013);
        }
    }

    @Override
    public ${entity}VO update(${entity}VO view) {
        TbSysDict entity = ${entity}VO.vo2Entity(view);
        if (!this.updateById(entity)) {
            throw new BizException(EnumCode.Err.ERR_5013);
        }
        return ${entity}VO.entity2VO(this.getById(entity.getId()));
    }

    @Override
    public ${entity}VO getViewById(Long id) {
        return ${entity}VO.entity2VO(this.getById(id));
    }
}
</#if>
